/**
 * Created by wang on 2016-8-30.
 * 同花顺本地服务
 */
define(['angular'], function (angular) {
    "use strict";

    var factory = function ($q,$rootScope, CommonService, LocalCacheService, WebService) {
        return {
            isTHS : function(){ //环境监测
                return LocalCacheService.getAppContextName() == "gcs_ths";
            },

            modifyPwd: function(userName, passWord){//修改密码
                var deferred = $q.defer();
                ThsTools.editePassword({userName : userName, passWord : passWord}, function (obj) {
                    deferred.resolve(obj);
                });
                return deferred.promise;
            },
            logout: function(){//退出
                var deferred = $q.defer();
                ThsTools.sdkLogout(function (obj) {
                    deferred.resolve(obj);
                });
                return deferred.promise;
            },
            loginThs : function(userName, passWord){ //登录
                var deferred = $q.defer();
                ThsTools.loginInfo({userName : userName, passWord : passWord}, function (obj) {
                    deferred.resolve(obj);
                });
                return deferred.promise;
            },
            /**
             * 获取当前用户
             *  KHH ----------客户号
                 Name ----------客户姓名
                 ZJZH ----------资金账号
                 HostType ----------账号类型（普通 0 信用 1 期货 2 港股 3 ）
                 SessionID ----------连接的session
             */
            getActivePtUser : function(){
                var deferred = $q.defer();
                ThsTools.loginInfo(function(data){
                    if(data &&data.result && data.result.token){
                        deferred.resolve(data);
                    } else {
                        deferred.reject(-1);//没有登录
                    }
                });
                return deferred.promise;
            },
            loginTap : function(account_content, func){
                var user = LocalCacheService.getUser();
                if(user && user.token) {
                    func();
                } else {
                    WebService.loginWithoutCaptcha({account_content:account_content.userName,password: account_content.passWord,
                        input_content : '1'}).then(function(data){
                        var user = {};
                        user.token = data.token;
                        LocalCacheService.setUser(user);
                        WebService.getUserInfo().then(
                            function (result){
                                LocalCacheService.setUser(result);
                                WebService.qryConduitInfo({mobile_tel: result.mobile_tel}).then(function(data){
                                    if(!CommonService.isStrEmpty(data.conduit_no)
                                        && CommonService.isStrEmpty(LocalCacheService.getRecommendInfos())){
                                        LocalCacheService.setRecommendInfos(data.conduit_no);
                                    }
                                });
                                func();
                            }, function(result){
                                LocalCacheService.removeUser();
                                CommonService.showConfig({message:"获取用户信息失败"});
                            }
                        )
                    });
                }
            },
            checkUser : function(func, func1){
                var _this = this;
                _this.getActivePtUser().then(function(data){
                    // _this.loginTap(data, func);
                    func();
                }, function(data){
                    if(data == -1) {
                        func1();
                    }
                })
            }
        }
    };

    factory.$inject = ['$q', '$rootScope','CommonService', 'LocalCacheService','WebService'];
    return factory;
});